package com.jiaming.restaurant.api.foodtable.controller;

import cn.hutool.core.util.StrUtil;
import com.jiaming.restaurant.api.common.controller.BaseController;
import com.jiaming.restaurant.api.common.response.ResultCode;
import com.jiaming.restaurant.api.foodtable.bean.entity.FoodTable;
import com.jiaming.restaurant.api.foodtable.service.IFoodTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dragon
 * @create 2020-12-29 11:28
 */
@RestController
@RequestMapping(value = "/food")
public class FoodTableController extends BaseController {

    @Autowired
    IFoodTableService foodTableService;

    @PostMapping(value = "/save")
    public Object save(@RequestBody FoodTable foodTable) {
        Map<String, Object> result = new HashMap<>();
        // 安全校验
//        String code = foodTable.getCode();
//        if (StrUtil.isEmpty(code)) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
//        Integer num = foodTable.getNum();
//        if (num == null || num <= 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
//        Integer restaurantId = foodTable.getRestaurantId();
//        if (restaurantId == null || restaurantId <= 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        boolean isOk = foodTableService.save(foodTable);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        result.put("data", foodTable.getId());
        return result;
    }

    @PostMapping(value = "/updateById")
    public Object update(@RequestBody FoodTable foodTable) {
        Map<String, Object> result = new HashMap<>();
//        Integer id = foodTable.getId();
//        if (id == null || id <= 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        boolean isOk = foodTableService.updateById(foodTable);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        return result;
    }

    @PostMapping(value = "/deleteById")
    public Object remove(@RequestParam Integer id) {
        Map<String, Object> result = new HashMap<>();
//        if (id == null || id <= 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        boolean isOk = foodTableService.removeById(id);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        return result;
    }

    @PostMapping(value = "/list")
    public Object list(@RequestBody FoodTable foodTable) {
        Map<String, Object> result = new HashMap<>();
        Integer restaurantId = foodTable.getRestaurantId();
        if (restaurantId == null || restaurantId <= 0) {
            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
            result.put("msg", "参数值不合法");
            return result;
        }
        List<FoodTable> list = foodTableService.list(foodTable);
        result.put("code", ResultCode.SUCCESS);
        result.put("data", list);
        return result;
    }

    @PostMapping(value = "/getByRestId")
    public Object list(@RequestParam Integer restId) {
        Map<String, Object> result = new HashMap<>();
        List<FoodTable> foodTables = foodTableService.getByRest(restId);
        result.put("code", ResultCode.SUCCESS);
        result.put("data", foodTables);
        return result;
    }

    @PostMapping(value = "/getById")
    public Object getById(@RequestParam Integer id) {
        Map<String, Object> result = new HashMap<>();
        FoodTable byId = foodTableService.getById(id);
        result.put("code", ResultCode.SUCCESS);
        result.put("data", byId);
        return result;
    }

    @GetMapping(value = "/find")
    public Object find() {
        Map<String, Object> result = new HashMap<>();
        List<FoodTable> list = foodTableService.list();
        result.put("code", ResultCode.SUCCESS);
        result.put("data", list);
        return result;
    }
}
